<?php

namespace Home\Controller;

use Think\Controller;

class LockedController extends BaseController
{
    /**
     * 存币提现
     * User: ming
     * Date: 2019/7/24 16:21
     */
    public function withdraw()
    {

        $lid = I('lid') ? I('lid') : 0;
        //用户id
        $user_id = $this->user_info['id'];
        //获存币数量
        $lockData = M('locked')->where(['userid' => $user_id, 'id' => $lid])->find();
        if ($lockData) {
//            $month_time = strtotime("+1 months", $lockData['addtime']);//下个月时间戳
//            dump($month_time);die;
//            if ($month_time >= time()) {
//                $lockData['time_status'] = 1;//未满一个月
//            } else {
//                $lockData['time_status'] = 2;//满一个月
//            }
            $this->assign("lockData", $lockData);//获取数据
        }
//       $month_time= date("Y-m-d H:i:s", strtotime("+1 months", $lockData['addtime']));//下个月时间
//        dump($month_time); die;
        //获取账户信息
        $userData = M('user')->where(['id' => $user_id])->find();
        $this->assign('userData', $userData);
        //获取手续费参数
        $configData = M('reward_config')->find();
        $this->assign('configData', $configData);

        if (IS_POST) {
//            if ($userData['is_real'] != 2) {
//                $this->ajaxReturn(array('code' => '-1', 'message' => '请先到个人中心完成实名认证'));
////                ajax_return(1, '请先到个人中心完成实名认证');
//            }

            //接收参数
            $data = I('post.');
            if ($userData['password2'] != MD5($data['password2'])) {
                $this->ajaxReturn(array('code' => '-1', 'message' => '请输入正确的支付密码'));
            }
//            if($configData['canshu69'] == 0 )
//            {
//                $this->ajaxReturn(array('code'=>'-1','message'=>$configData['canshu70']));
//            }
//            if ($data['is_real'] != 2) {
//                $this->ajaxReturn(array('code' => '-2', 'message' => '请先完成实名认证'));
//            }
            $lockData = M('locked')->where(['userid' => $user_id, 'id' => $data['lid']])->find();
            if($lockData['status']=='1'){
                $this->ajaxReturn(array('code' => '-1', 'message' => '已经提取完成'));
            }
            $time=3600*24*30;
            if(($lockData['addtime']+$time*6)<time()){
                $charge =$configData['a19'];
            }elseif (($lockData['addtime']+$time*5)<time()){
                $charge =$configData['a18'];
            }elseif (($lockData['addtime']+$time*4)<time()){
                $charge =$configData['a17'];
            }elseif (($lockData['addtime']+$time*3)<time()){
                $charge =$configData['a16'];
            }elseif (($lockData['addtime']+$time*2)<time()){
                $charge =$configData['a15'];
            }else{
                $charge =$configData['a14'];
            }
            if ($data['amount'] > $lockData['amount']) {
                $this->ajaxReturn(array('code' => '-1', 'message' => '账户余额不足'));
            }

            //减去账户余额
            $balance = $lockData['amount']*(1 - $charge);
            $price_from = "z" . $data['from'];//币种选择
            M()->startTrans();
            //提取为0结束此存币信息可删除
            $res1=M('locked')->where(['id' => $data['lid']])->save(['status' => 1]);//已结束

            $res2 = M('user')->where(['id' => $user_id])->setInc($price_from, $balance);//本金归还
            $res3 = $this->add_fmoney_log($data['lid'], $user_id, $balance, $data['from'], '本金归还',  "12", $charge);
            $res4 = M('user')->where(['id' => $user_id])->setDec('z14', $lockData['amount']);//存币总额修改

            if ($res1&&$res2 && $res3 && $res4) {
                M()->commit();
                $db = M();
                $db->execute("call kt002($user_id,$balance)");
                //超过额度
                $db->execute("call up_level($user_id)");
//                if($userData['zuo_zong']>=$configData['a22']){
//                    //代理升级
//                    $this->reward($configData,$userData);
//                }
                $this->ajaxReturn(array('code' => '1', 'message' => '提取成功'));
            } else {
                M()->rollback();
                $this->ajaxReturn(array('code' => '-1', 'message' => '提取失败'));
            }

        } else {
            //获取存币生息提现记录
//            $where['user_id'] = $user_id;
//            $where['from_id'] = $lid;
//            $where['type'] = ['in', [7, 11]];//存币生息ASSET收益EASY收益
////            $where['reward_type'] = 712;
//            $where['reward_type'] = ['in', [712, 1112]];
//            $count = M('fmoney')->where($where)->count();
//            $Page = new \Think\Page($count, 4);
////            $Page->setConfig(prev, '上一页');
////            $Page->setConfig(next, '下一页');
//            $show = $Page->show();
//            $data = M('fmoney')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
//            $this->assign('chargeData', $data);
//            $this->assign('page', $show);
            $this->display();
        }
    }
//    public function withdraw()
//    {
//        $lid = I('lid') ? I('lid') : 0;
//        //用户id
//        $user_id = $this->user_info['id'];
//        //获存币数量
//        $lockData = M('locked')->where(['userid' => $user_id, 'id' => $lid])->find();
//        if ($lockData) {
//            $month_time = strtotime("+1 months", $lockData['addtime']);//下个月时间戳
//            if ($month_time >= time()) {
//                $lockData['time_status'] = 1;//未满一个月
//            } else {
//                $lockData['time_status'] = 2;//满一个月
//            }
//            $this->assign("lockData", $lockData);//获取数据
//        }
////       $month_time= date("Y-m-d H:i:s", strtotime("+1 months", $lockData['addtime']));//下个月时间
////        dump($month_time); die;
//        //获取账户信息
//        $userData = M('user')->where(['id' => $user_id])->find();
//        $this->assign('userData', $userData);
//        //获取手续费参数
//        $configData = M('reward_config')->find();
//        $this->assign('configData', $configData);
//
//        $coin_asset = $this->asset_price();//ASSET实时价格
//        $coin_easy=$this->easy_price();//easy实时价格
//        $this->assign('coin_easy', $coin_easy);
//        //asset实时价格
//        $this->assign('coin_asset', $coin_asset);
//        if (IS_POST) {
////            if ($userData['is_real'] != 2) {
////                $this->ajaxReturn(array('code' => '-1', 'message' => '请先到个人中心完成实名认证'));
//////                ajax_return(1, '请先到个人中心完成实名认证');
////            }
//
//            //接收参数
//            $data = I('post.');
//
////            if($configData['canshu69'] == 0 )
////            {
////                $this->ajaxReturn(array('code'=>'-1','message'=>$configData['canshu70']));
////            }
////            if ($data['is_real'] != 2) {
////                $this->ajaxReturn(array('code' => '-2', 'message' => '请先完成实名认证'));
////            }
//            $lockData = M('locked')->where(['userid' => $user_id, 'id' => $data['lid']])->find();
//
//            if ($data['amount'] == '' || $data['amount'] <= 0) {
//                $this->ajaxReturn(array('code' => '-1', 'message' => '信息不完整'));
//            }
//            $month_time = strtotime("+1 months", $lockData['addtime']);//下个月时间戳
//            if ($month_time >= time()) {
//                $charge = $data['amount'] * $configData['p4'];//未满一个月手续费
//            } else {
//                $charge = $data['amount'] * $configData['p5'];//满一个月手续费
//
//            }
//            if ($data['amount'] > $lockData['amount']) {
//                $this->ajaxReturn(array('code' => '-1', 'message' => '账户余额不足'));
//            }
//            //提取为0结束此存币信息可删除
//            if (($lockData['amount'] - $data['amount']) <= 0) {
//                M('locked')->where(['id' => $data['lid']])->save(['status' => 1]);//已结束
//            }
//            //减去账户余额
//            $res1 = M('locked')->where(['id' => $data['lid']])->setDec('amount', $data['amount']);
//            $balance = $data['amount'] - $charge;
//
//            $price_from = "z" . $data['from'];//币种选择
//
////                $res2 = M('user')->where(['id' => $user_id])->setInc('z7', $balance);//本金归还ASSET
////                $res3 = $this->add_log($data['lid'],$user_id , $balance, 7, '本金提取', "712", $charge);
//
//            $res2 = M('user')->where(['id' => $user_id])->setInc($price_from, $balance);//本金归还ASSET
////            $res3 = $this->add_log($data['lid'], $user_id, $balance, $price_from, '本金归还', $price_from . "12", $charge);
//            $res3 = $this->add_log($data['lid'], $user_id, $balance, $data['from'], '本金归还', $data['from'] . "12", $charge);
//
//            if ($data['from'] == 7) {
//                $res4 = M('user')->where(['id' => $user_id])->setDec('z10', $data['amount']);//存币ASSET总额修改
//
//            } elseif ($data['from'] == 11) {
//                $res4 = M('user')->where(['id' => $user_id])->setDec('z13', $data['amount']);//存币EASY总额修改
//            }
//
//            if ($res1 && $res2 && $res3 && $res4) {
//                $this->ajaxReturn(array('code' => '1', 'message' => '提取成功'));
//            } else {
//                $this->ajaxReturn(array('code' => '-1', 'message' => '提取失败'));
//            }
//
//        } else {
//            //获取存币生息提现记录
//            $where['user_id'] = $user_id;
//            $where['from_id'] = $lid;
//            $where['type'] = ['in', [7, 11]];//存币生息ASSET收益EASY收益
////            $where['reward_type'] = 712;
//            $where['reward_type'] = ['in', [712, 1112]];
//            $count = M('fmoney')->where($where)->count();
//            $Page = new \Think\Page($count, 4);
////            $Page->setConfig(prev, '上一页');
////            $Page->setConfig(next, '下一页');
//            $show = $Page->show();
//            $data = M('fmoney')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
//            $this->assign('chargeData', $data);
//            $this->assign('page', $show);
//            $this->display();
//        }
//    }
    /**
     * 锁仓显示页面
     */
    public function index()
    {
        if (IS_POST) {
            //接收数据
            $data = I('post.');

            //用户id
            $user_id = $this->user_info['id'];

            //判断是否为空
            if ($data['amount'] == '') {
                $this->ajaxReturn(array('code' => '-1', 'message' => '锁仓数量不能为空'));
            }

            //获取用户信息
            $userData = M('user')->where(['id' => $user_id])->find();

            //判断账户余额是否足够
            if ($data['amount'] > $userData['z1']) {
                $this->ajaxReturn(array('code' => '-1', 'message' => '账户余额不足'));
            }

            //判断是否有锁仓记录
            $typeData = M('locked')->where(['userid' => $user_id])->find();

            //如果一条没有直接插入即可
            if (empty($typeData)) {
                //判断锁仓数量
                if ($data['amount'] < 1000) {
                    $this->ajaxReturn(array('code' => '-1', 'message' => '锁仓数量不能小于1000'));
                }

                //锁仓入库
                $data['userid'] = $user_id;
                $data['addtime'] = time();

                $res1 = M('locked')->add($data);

                //锁仓记录入库

                $res2 = $this->add_log($user_id, $user_id, $data['amount'], 7, '用户锁仓');
                //减去账户余额
                $res3 = M('user')->where(['id' => $user_id])->setDec('z1', $data['amount']);

                if ($res1 && $res2 && $res3) {
                    $this->ajaxReturn(array('code' => '1', 'message' => '锁仓成功'));
                } else {
                    $this->ajaxReturn(array('code' => '-1', 'message' => '锁仓失败'));
                }

            } else {
                if ($data['amount'] <= 0) {
                    $this->ajaxReturn(array('code' => '-1', 'message' => '锁仓数量不能为0'));
                }

                $where['userid'] = $user_id;
                $info['amount'] = $typeData['amount'] + $data['amount'];

                $res1 = M('locked')->where($where)->save($info);
                //插入追加记录
                $res2 = $this->add_log($user_id, $user_id, $data['amount'], 7, '用户追加锁仓');
                //减去账户余额
                $res3 = M('user')->where(['id' => $user_id])->setDec('reward1', $data['amount']);

                if ($res1 && $res2 && $res3) {
                    $this->ajaxReturn(array('code' => '1', 'message' => '追加锁仓成功'));
                } else {
                    $this->ajaxReturn(array('code' => '-1', 'message' => '追加锁仓失败'));
                }
            }

        } else {
            //获取锁仓参数
            $configData = M('reward_config')->find();
            $this->assign('configData', $configData);

            //获取用户信息
            $user_id = $this->user_info['id'];

            $userData = M('user')->where(['id' => $user_id])->find();
            $this->assign('userData', $userData);

            //获取分红记录
            $map1['from_id'] = $user_id;
            $map1['reason'] = '分红';
            $cashData = M('fmoney')->where($map1)->select();
            $this->assign('cashData', $cashData);

            //获取锁仓数量
            $lockAmount = M('locked')->where(['userid' => $user_id])->sum(amount);
            $this->assign('lockAmount', $lockAmount);

            //获取用户昨天所有分红
            $yesterday = strtotime(date("Y-m-d", strtotime("-1 day"))); //昨天
            $today = strtotime(date("Y-m-d"));
            $map4['from_id'] = $user_id;
            $map4['transaction_time'] = array('between', array($yesterday, $today));;
            $map4['reward_type'] = 001;
            $yesterdayUserData = M('fmoney')->where($map4)->sum(amount);
            $this->assign('yesterdayUserData', $yesterdayUserData);

            //获取用户本月所有分红
            $usercurrentstart = strtotime(date('Y-m-01 00:00:00'));
            $usercurrentend = strtotime(date('Y-m-d H:i:s'));
            $map8['from_id'] = $user_id;
            $map8['reward_type'] = 001;
            $map8['transaction_time'] = array('between', array($usercurrentstart, $usercurrentend));
            $usercurrentMonth = M('fmoney')->where($map8)->sum(amount);
            $this->assign('usercurrentMonth', $usercurrentMonth);

            //获取用户今天所有分红
            $today = strtotime(date("Y-m-d")); //今天
            $map5['from_id'] = $user_id;
            $map5['transaction_time'] = array('egt', $today);
            $map5['reward_type'] = 001;
            $todayUserData = M('fmoney')->where($map5)->sum(amount);
            $this->assign('todayUserData', $todayUserData);

            //获取btc实时价格
            $btcPrice = M('reward_config')->find();
            $this->assign('btcPrice', $btcPrice['canshu60']);

            $this->display();
        }

    }


    /**
     * 锁仓记录
     */
    public function record()
    {
        //用户id
        $user_id = $this->user_info['id'];

        //获取锁仓记录
        $where['from_id'] = $user_id;
        $where['type'] = array('in', '7,8,9');
        $count = M('fmoney')->where($where)->count();
        $Page = new \Think\Page($count, 8);
        $Page->setConfig(prev, '上一页');
        $Page->setConfig(next, '下一页');
        $show = $Page->show();
        $data = M('fmoney')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $this->assign('lockData', $data);
        $this->assign('page', $show);

        $this->display();
    }

    /**
     * 存币信息列表
     * User: ming
     * Date: 2019/7/24 18:14
     */
    public function locked_list()
    {
        //用户id
        $user_id = $this->user_info['id'];

        //获取锁仓记录
        $where['userid'] = $user_id;
        $count = M('locked')->where($where)->count();
        $Page = new \Think\Page($count, 8);
//        $Page->setConfig(prev, '上一页');
//        $Page->setConfig(next, '下一页');
        $show = $Page->show();
        $data = M('locked')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $this->assign('lockData', $data);
        $this->assign('page', $show);

        $this->display();
    }

    /**
     * 佣金提现记录
     * User: ming
     * Date: 2019/9/3 14:05
     */
    public function commission(){
        //用户id
        $user_id = $this->user_info['id'];

        $where['user_id'] = $user_id;
        $where['tips'] = '佣金提取';
        $count = M('fmoney')->where($where)->count();
        $Page = new \Think\Page($count, 8);
//        $Page->setConfig(prev, '上一页');
//        $Page->setConfig(next, '下一页');
        $show = $Page->show();
        $data = M('fmoney')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $this->assign('data', $data);
        $this->assign('page', $show);

        $this->display();
    }
}